<template lang="pug">
.project-association
  mt-dialog(
    v-model="model"
    cancel-text="取消"
    confirm-text="保存"
    :on-close="updateModel"
    :on-cancel="updateModel"
    :on-confirm="onConfirm"
    :confirm-loading="loadingMap.combinedRelatedUserGroup"
    title="关联检验人员"
    width="800"
  )
    data-page(
      v-bind="pageParams"
    )
      mt-table(
        v-bind="tableProps"
      )
  </template>

<script setup lang="ts">
import { type QueryPagination, useApi } from "@scmt/core-api";
import {
  type FormItemProps,
  type TableProps,
  type DataPageProps,
  useNotify,
} from "@scmt/common-components";
import type { UserEntity, UserQueryParams } from "@scmt/api-user";
import { type InspectGroupEntity } from "@scmt/api-lis-inspect-group";
import { useRequest } from "@scmt/common-hooks";
const $api = useApi();
const $notify = useNotify();
const projectList = ref<Array<UserEntity>>([]);
const queryPramas = reactive<UserQueryParams>({
  pageNum: 1,
  pageSize: 20,
});
const updateModel = () => {
  model.value = false;
};
const emit = defineEmits(["message-to-parent"]);
const onConfirm = () => {
  if (projectList.value.length < 1) {
    $notify.error("请选择需要添加的用户");
    return;
  }
  const projectIdData = [].concat(projectList.value).map((item) => {
    return item.userId;
  });
  request("combinedRelatedUserGroup", {
    groupId: props.selectEquipment?.id ?? "",
    userIdList: projectIdData,
  });
};
const model = defineModel({
  default: false,
});

const { request, data, loadingMap } = useRequest({
  tableData: (q: any) => {
    let params = Object.assign(
      { groupId: props.selectEquipment?.value?.id },
      queryPramas,
      q
    );
    if (!props.selectEquipment || !props.selectEquipment.id) {
      params = Object.assign({ portfolioProjectId: "testtt" }, queryPramas, q);
    } else {
      params = Object.assign(
        { groupId: props.selectEquipment?.id },
        queryPramas,
        q
      );
    }
    return $api.user.getNotRelatedGroupListByPage(params);
  },
  combinedRelatedUserGroup: (params: any) => {
    return $api.lis.userGroup.combinedRelatedUserGroup(params).then(() => {
      $notify.success("添加医生成功");
      model.value = false;
      emit("message-to-parent");
    });
  },
});
const pageParams = computed<DataPageProps>(() => {
  const queryFields: FormItemProps[] = [
    {
      prop: "username",
      label: "姓名",
    },
  ];

  const dataPageProps: DataPageProps = {
    loading: loadingMap.tableData,
    queryModel: queryPramas,
    queryFields,
    onQuery: () => request("tableData"),
    columns: 3,
  };

  return dataPageProps;
});

const props = defineProps({
  selectEquipment: {
    type: Object as PropType<InspectGroupEntity>,
    default: () => {},
  },
});
const tableProps = computed(() => {
  const { tableData } = toRefs(data);

  const tableProps: TableProps = {
    loading: loadingMap.tableData,
    data: tableData.value?.items,
    pagination: tableData.value?.pagination,
    columns: [
      {
        type: "selection",
        label: "序号",
        width: "60px",
      },
      {
        prop: "jobNumber",
        label: "工号",
      },
      {
        prop: "username",
        label: "姓名",
      },
      {
        prop: "phone",
        label: "手机号",
      },
    ],
    onPaginate: (pagination: QueryPagination) =>
      request("tableData", pagination),
    onSelectionChange: (row: any) => {
      projectList.value = [];
      projectList.value = row;
    },
  };
  return tableProps;
});
watch(
  () => model.value,
  (item) => {
    if (item) {
      queryPramas.username = "";
      request("tableData");
    }
  },
  {
    immediate: true,
  }
);
</script>

<style lang="less" scoped></style>
